package suit.ui;


public class ListItem extends Widget {

	private boolean active;
	private boolean pressed;
	
	public ListItem(Widget w) {
		add(w);
	}

	Rectangle getRequisition() {
		return ((Widget) children.firstElement()).getRequisition();
	}

	protected boolean onExpose(Event e) {
		Widget w = ((Widget) children.firstElement());
		e.theme.drawListItem(e.graphics, this, allocation, active);
		w.sizeAllocate(allocation);
		w.onExpose(e);
		return false;
	}
	
	private void setActive(boolean active) {
		this.active = active;
		invalidate(this);
	}
	
	protected boolean onPointerPressed(Event e) {
		if (localizeEvent(e) != null) {
			pressed = true;
			setActive(true);
		}
		return false;
	}
	
	protected boolean onPointerDragged(Event e) {
		pressed = false;
		if (this.active) {
			setActive(false);
		}
		return false;
	}

	protected boolean onPointerReleased(Event e) {
		if( pressed ) {
			if (clickedHandler != null)
				clickedHandler.onClicked(this);
		}
		pressed = false;
		if (this.active) {
			setActive(false);
		}
		return false;
	}

	private Clicked clickedHandler;

	public void connect(Clicked handler) {
		clickedHandler = handler;
	}

	interface Clicked {
		public void onClicked(ListItem source);
	}
	
}